namespace Resonator.Tests.Models
{
	using Castle.ActiveRecord;
	using Castle.ActiveRecord.Framework;
	using Castle.ActiveRecord.Framework.Config;
	using NHibernate;
	using NHibernate.Cfg;
	using NHibernate.Search.Engine;
	using NHibernate.Search.Impl;
	using NUnit.Framework;
	using Domain.Models;

	[TestFixture]
	[Category("Database")]
	public class ModelTester
	{
		static ModelTester()
		{
			log4net.Config.XmlConfigurator.Configure(); 
 
			// Initialize Database
			ActiveRecordStarter.ResetInitializationFlag();
			ActiveRecordStarter.Initialize(typeof(Resource).Assembly, ActiveRecordSectionHandler.Instance);
			ActiveRecordStarter.DropSchema();
			ActiveRecordStarter.CreateSchema();

			// Initialize Lucene
			ISessionFactoryHolder holder = ActiveRecordMediator.GetSessionFactoryHolder();
			Configuration configuration = holder.GetConfiguration(typeof(ActiveRecordBase));
			ISessionFactory sessionFactory = holder.GetSessionFactory(typeof(ActiveRecordBase));
			SearchFactory.Initialize(configuration, sessionFactory);
			InterceptorFactory.Create = delegate { return new SearchInterceptor(); };
		}

		private ISessionScope _session;
		protected virtual ISessionScope Session
		{
			get { return _session; }
		}

		[SetUp]
		public virtual void SetUp()
		{
			_session = new SessionScope(); 
		}

		[TearDown]
		public virtual void TearDown()
		{
			if( _session != null )
			{
				_session.Dispose();
			}
		}
	}
}